package ru.ng.sample.ui.settings;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.LocaleResolver;
import ru.ng.sample.core.user.ProfileManager;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * User: ngorosh
 * Date: 22.05.11
 * Time: 15:31
 */
public class UserProfileLocaleResolver
    implements LocaleResolver
{
    @Autowired
    private ProfileManager profileManager;

    @Override
    public Locale resolveLocale( HttpServletRequest request )
    {
        String lang = profileManager.getUserLang();
        if ( lang != null )
        {
            return new Locale( lang );
        }

        return request.getLocale();
    }

    @Override
    public void setLocale( HttpServletRequest request, HttpServletResponse response, Locale locale )
    {
        if ( locale != null )
        {
            profileManager.saveUserLang( locale.getLanguage() );
        }
    }
}
